﻿// RealTimeChromG.cpp: 实现文件
//

#include "stdafx.h"
#include "DC-QC.h"
#include "RealTimeChromG.h"


// CRealTimeChromG

IMPLEMENT_DYNAMIC(CRealTimeChromG, CWnd)

CRealTimeChromG::CRealTimeChromG()
{
	m_nMaxYA = 10;
	m_nMaxYB = 10;
	m_nXScale = 30;		//30 add by zhupj
	m_nZoomMode = 1;
	m_nMinYA = 0;
	m_nMinYB = 0;
	m_bMouseDown = FALSE;
	m_nTicks = 0;
}

CRealTimeChromG::~CRealTimeChromG()
{
}


BEGIN_MESSAGE_MAP(CRealTimeChromG, CWnd)
	ON_WM_PAINT()
	ON_WM_LBUTTONDOWN()
	ON_WM_LBUTTONUP()
	ON_WM_MOUSEMOVE()
	ON_WM_CREATE()
	ON_WM_TIMER()
END_MESSAGE_MAP()



// CRealTimeChromG 消息处理程序
void CRealTimeChromG::OnPaint()
{
	CPaintDC dc(this); // device context for painting
					   // TODO: 在此处添加消息处理程序代码
					   // 不为绘图消息调用 CWnd::OnPaint()
	CRect rect;
	GetClientRect(&rect);

	CBitmap ClientScreen, *pOldBitmap;
	CRect UpdateRect(rect);

	CDC MDC;
	MDC.CreateCompatibleDC(&dc);
	ClientScreen.CreateCompatibleBitmap(&dc, rect.Width(), rect.Height());
	pOldBitmap = MDC.SelectObject(&ClientScreen);
	MDC.FillSolidRect(UpdateRect, RGB(255, 255, 255));

	Draw(&MDC, rect);

	dc.BitBlt(UpdateRect.left, UpdateRect.top,
		UpdateRect.right - UpdateRect.left,
		UpdateRect.bottom - UpdateRect.top,
		&MDC, UpdateRect.left, UpdateRect.top, SRCCOPY);
	MDC.SelectObject(pOldBitmap);

	ClientScreen.DeleteObject();
	MDC.DeleteDC();
}


void CRealTimeChromG::Draw(CDC* pDC, CRect rt)
{
	try
	{
		DrawInBorder(pDC, rt);
		rt.DeflateRect(-1, -1, 1, 1);
		BxGradientFill(pDC, rt, BACKCOLOR1, BACKCOLOR2, 1);
		rt.DeflateRect(60*g_DPIScaleX, 10*g_DPIScaleY, 10*g_DPIScaleX, 20*g_DPIScaleY);
		CFont* pF = pDC->SelectObject(&gl.m_pSmallFont);
		DrawFrame(rt, pDC);
		DrawCurve(rt, pDC);
		pDC->SelectObject(pF);
	}
	catch (...)
	{
	}
}

void CRealTimeChromG::DrawFrame(CRect rt, CDC *pDC)
{
	int	m_left = rt.left;
	int	m_top = rt.top;
	int	m_right = rt.right;
	int	m_bottom = rt.bottom;


	//创建画笔对象
	CPen pPenBlack(PS_SOLID, 2, RGB(255, 0, 0));
	CPen pPenG(PS_DOT, 1, RGB(5, 85, 5));
	CFont* pF = (CFont*)pDC->SelectObject(&font); //保存当前字体
	pDC->SetBkMode(TRANSPARENT);

	pDC->SetTextColor(RGB(255, 128, 0));

	//绘制A窗口线
	int nLastP = 0;
	CPoint pt;
	int nWidth = rt.Width();
	int nHeight = rt.Height();
	int nOldDrawMode = pDC->SetROP2(R2_XORPEN);
	int CIndex, LIndex, RIndex;
	for (int jj = 0; jj < 7; jj++)
	{

		CIndex = gl.gcCi.scA[jj].RealyTime*DATAFREQUENCE;
		if (nLastP == 0)
			LIndex = CIndex - gl.gcCi.scA[jj].Wrap*DATAFREQUENCE / 2;
		else
		{
			if (CIndex - gl.gcCi.scA[jj].Wrap*DATAFREQUENCE / 2 < nLastP)
				LIndex = nLastP;
			else
				LIndex = CIndex - gl.gcCi.scA[jj].Wrap*DATAFREQUENCE / 2;
		}
		RIndex = CIndex + gl.gcCi.scA[jj].Wrap*DATAFREQUENCE / 2;
		if (RIndex >= gl.MAXPOINTS) RIndex = gl.MAXPOINTS - 1;
		nLastP = RIndex;

		pt.x = rt.left + LIndex * nWidth / gl.MAXPOINTS;
		pt.y = rt.bottom - 2*g_DPIScaleY;
		int nAWidth = (RIndex - LIndex) * nWidth / gl.MAXPOINTS;
		int nAHeight = nHeight - 4*g_DPIScaleY;
		CRect rtChromA = CRect(pt, CSize(nAWidth, (-1)*nAHeight));


		//int oldBkMode = pDC->SetBkMode(OPAQUE); 
		//pDC->FillSolidRect(rtChromA, RGB(0, 200, 0)); // Use solid color to fill the rectangle
		//CBrush fillBrush(RGB(0, 200, 0)); // 创建一个红色的填充画刷
		//pDC->SelectObject(&fillBrush);    // 选择填充画刷
		//pDC->Rectangle(rtChromA);         // 绘制矩形，填充颜色为红色
		//pDC->SetBkMode(oldBkMode);


		CString strTemp;
		//strTemp.Format(_T("%s=%.2f"),gl.curChromA.ChS[jj].Name, CIndex*30.0/gl.MAXPOINTS); 
		strTemp.Format(_T("%s"), gl.gcCi.scA[jj].strName);
		pt.x = rt.left + CIndex * nWidth / gl.MAXPOINTS;
		pDC->TextOut(pt.x, pt.y - 14*g_DPIScaleY, strTemp);

	}
	pDC->SetROP2(nOldDrawMode);

	pDC->SetTextColor(RGB(255, 255, 255));
	CPen* pOldPen = pDC->SelectObject(&pPenBlack);

	// 绘制X轴刻度
	int nTemp;
	CString strTemp;
	CRect rect1(0, 0, 0, 0);

		// 创建字体对象
	CFont font;
	font.CreateFont(
		20,                        // 字体高度（单位为逻辑单元）
		0,                         // 字符的平均宽度
		0,                         // 文本的倾斜角度
		0,                         // 基线的倾斜角度
		FW_MEDIUM,                 // 字体粗细
		FALSE,                     // 是否斜体
		FALSE,                     // 是否下划线
		0,                         // 是否删除线
		ANSI_CHARSET,              // 字符集
		OUT_DEFAULT_PRECIS,        // 输出精度
		CLIP_DEFAULT_PRECIS,       // 裁剪精度
		DEFAULT_QUALITY,           // 输出质量
		DEFAULT_PITCH | FF_SWISS,  // 字体族和间距
		_T("Arial"));              // 字体名称

	// 保存原字体
	CFont* pOldFont = pDC->SelectObject(&font);
	COLORREF textColor = RGB(0, 0, 0); // 设置字体颜色为蓝色
	int oldBkMode = pDC->SetTextColor(textColor); // 设置字体颜色

	for (int i = 0; i <= gl.gcMachine.nXScale; i++)
	{
		nTemp = (m_right - m_left)*i / gl.gcMachine.nXScale + m_left;
		strTemp.Format(_T("%d"), i);
		pDC->MoveTo(nTemp, m_bottom);
		if (i % 5 == 0)
		{
			pDC->SelectObject(&pPenG);
			pDC->LineTo(nTemp, m_top);
			pDC->SelectObject(&pPenBlack);
		}
		else
			pDC->LineTo(nTemp, m_bottom - 4);

		pDC->SelectObject(pPenBlack);
		if (i % 5 == 0)
			pDC->ExtTextOut(nTemp - 6*g_DPIScaleX, m_bottom + 2*g_DPIScaleY, ETO_OPAQUE, rect1, strTemp, NULL);
	}
	pDC->SetBkMode(oldBkMode);

		// 恢复原字体
	pDC->SelectObject(pOldFont);

	// 删除字体对象
	font.DeleteObject();

	CPen  pPenY1(PS_SOLID, 2, gl.gcMachine.nLineColor[0]);
	pDC->SelectObject(&pPenY1);
	pDC->SetTextColor(gl.gcMachine.nLineColor[0]);
	// 绘制A Y轴网格
	pDC->MoveTo(rt.TopLeft());
	pDC->LineTo(m_left, m_bottom);
	pDC->LineTo(rt.BottomRight());
	for (int i = 1; i <= 10; i++)
	{
		nTemp = -(m_bottom - m_top)*i / 10 + m_bottom;
		strTemp.Format(_T("%.0f"), m_nMinYA + (m_nMaxYA - m_nMinYA)*i / 10.0);
		pDC->SelectObject(&pPenG);
		pDC->MoveTo(m_left, nTemp);
		pDC->LineTo(m_right, nTemp);
		pDC->SelectObject(&pPenBlack);
		if (i == 0) continue;
		int nL = strTemp.GetLength();
		pDC->ExtTextOut(m_left - 8 * nL*g_DPIScaleX, nTemp - 6*g_DPIScaleY, ETO_OPAQUE, rect1, strTemp, NULL);
		if (i == 10) 		pDC->ExtTextOut(m_left + 12*g_DPIScaleX, nTemp - 6*g_DPIScaleY, ETO_OPAQUE, rect1, _T("组份(mV)"), NULL);
	}
	// 绘制B Y轴网格
	CPen pPenY2(PS_SOLID, 2, gl.gcMachine.nLineColor[2]);
	pDC->SelectObject(&pPenY2);
	pDC->SetTextColor(gl.gcMachine.nLineColor[2]);
	pDC->MoveTo(rt.right, rt.top);
	pDC->LineTo(rt.BottomRight());
	m_left = rt.right;
	for (int i = 0; i <= 10; i++)
	{
		nTemp = -(m_bottom - m_top)*i / 10 + m_bottom;
		strTemp.Format(_T("%.0f"), m_nMinYB + (m_nMaxYB - m_nMinYB)*i / 10.0);
		pDC->MoveTo(m_left, nTemp);
		pDC->LineTo(m_left - 8, nTemp);
		if (i == 0) continue;
		int nL = strTemp.GetLength();
		pDC->ExtTextOut(m_left - 9 * nL*g_DPIScaleX, nTemp - 6*g_DPIScaleY, ETO_OPAQUE, rect1, strTemp, NULL);
		if (i == 10) pDC->ExtTextOut(m_left - 102*g_DPIScaleX, nTemp - 6*g_DPIScaleY, ETO_OPAQUE, rect1, _T("全烃(mV)"), NULL);
	}

	/*CPen  pPenGrid1(PS_SOLID, 1, gl.gcMachine.nLineColor[0]);
	pDC->SelectObject(&pPenGrid1);
	pDC->MoveTo(m_right - 100, m_top + 20);
	pDC->LineTo(m_right - 70, m_top + 20);
	pDC->SetTextColor(gl.gcMachine.nLineColor[0]);
	pDC->ExtTextOut(m_right - 66, m_top + 12, ETO_OPAQUE, rect1, _T("组份A"), NULL);
	m_top = m_top + 20;
	CPen  pPenGrid2(PS_SOLID, 1, gl.gcMachine.nLineColor[1]);
	pDC->SelectObject(&pPenGrid2);
	pDC->MoveTo(m_right - 100, m_top + 20);
	pDC->LineTo(m_right - 70, m_top + 20);
	pDC->SetTextColor(gl.gcMachine.nLineColor[1]);
	pDC->ExtTextOut(m_right - 66, m_top + 12, ETO_OPAQUE, rect1, _T("组份B"), NULL);
	CPen pPenGrid3(PS_SOLID, 1, gl.gcMachine.nLineColor[2]);
	pDC->SelectObject(&pPenGrid3);
	pDC->MoveTo(m_right - 100, m_top + 40);
	pDC->LineTo(m_right - 70, m_top + 40);
	pDC->SetTextColor(gl.gcMachine.nLineColor[2]);
	pDC->ExtTextOut(m_right - 66, m_top + 32, ETO_OPAQUE, rect1, _T("全烃"), NULL);*/

	m_left = rt.left;
	m_top = m_top - 20;
	//写当前采集的MV值
	CString strTempA;

	pDC->SetTextColor(gl.gcMachine.nLineColor[0]);
	strTempA.Format(_T("%-6s= %.4fmV"), _T("组份A"), gl.gcStatus.dFidAV);  // %6s确保左对齐，宽度为6
	pDC->ExtTextOut(m_left + 20 * g_DPIScaleX, m_top + 30 * g_DPIScaleY, ETO_OPAQUE, rect1, strTempA, NULL);

	pDC->SetTextColor(gl.gcMachine.nLineColor[1]);
	strTempA.Format(_T("%-6s= %.4fmV"), _T("组份B"), gl.gcStatus.dFidBV);  // 使用相同的格式确保对齐
	pDC->ExtTextOut(m_left + 20 * g_DPIScaleX, m_top + 50 * g_DPIScaleY, ETO_OPAQUE, rect1, strTempA, NULL);

	pDC->SetTextColor(gl.gcMachine.nLineColor[2]);
	strTempA.Format(_T("%-6s= %.4fmV"), _T("全烃"), gl.gcStatus.dFidCV);  // 相同格式
	pDC->ExtTextOut(m_left + 20 * g_DPIScaleX, m_top + 70 * g_DPIScaleY, ETO_OPAQUE, rect1, strTempA, NULL);

	pDC->SetTextColor(RGB(128, 255, 255));
	strTempA.Format(_T("%-7s= %.2fmV"), _T("Co2"), gl.gcStatus.dCO2V);  // CO2 也保持一致
	pDC->ExtTextOut(m_left + 20 * g_DPIScaleX, m_top + 90 * g_DPIScaleY, ETO_OPAQUE, rect1, strTempA, NULL);


	pDC->SelectObject(pOldPen);
	pDC->SelectObject(pF);		//恢复以前的字体
}


void CRealTimeChromG::DrawCurve(CRect rt, CDC *pDC)
{
	CFont   font;
	CFont   *pOldFont;
	font.CreateFont(12, 0, 2700, 2700, FW_NORMAL,
		0, 0, 0, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS,
		CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,
		DEFAULT_PITCH, _T("@宋体"));
	pOldFont = pDC->SelectObject(&font);

	CPoint pt;
	CPen* pOldPen = NULL;
	int nWidth = rt.Width();
	int nHeight = rt.Height();
	//
	double nMaxAY = m_nMaxYA;
	double nMaxBY = m_nMaxYB;
	double nMinY = 0;

	CPen pPenA(PS_SOLID, gl.gcMachine.nLineWidth[0], gl.gcMachine.nLineColor[0]);
	CPen pPenB(PS_SOLID, gl.gcMachine.nLineWidth[1], gl.gcMachine.nLineColor[1]);
	CPen pPenC(PS_SOLID, gl.gcMachine.nLineWidth[2], gl.gcMachine.nLineColor[2]);

	for (int nCh = 0; nCh < 3; nCh++)
	{
		if (nCh == 0)
		{
			if (gl.m_nWorkMode == 2) continue;
			nMinY = m_nMinYA;
			pOldPen = pDC->SelectObject(&pPenA);
			pDC->SetTextColor(gl.gcMachine.nLineColor[0]);
		}
		else if (nCh == 1)
		{
			if (gl.m_nWorkMode == 1) continue;
			nMinY = m_nMinYA;
			pOldPen = pDC->SelectObject(&pPenB);
			pDC->SetTextColor(gl.gcMachine.nLineColor[1]);
		}
		else
		{
			nMinY = m_nMinYB;
			pOldPen = pDC->SelectObject(&pPenC);
			pDC->SetTextColor(gl.gcMachine.nLineColor[2]);
		}

		for (int i = 0; i < gl.MAXPOINTS; i++)
		{
			pt.x = rt.left + i * nWidth / gl.MAXPOINTS;
			if (gl.m_nWorkMode == 2)
			{
				if (nCh == 1)
				{
					pt.y = rt.bottom - (gl.curChromB.CA[i] - nMinY)*nHeight*1.0 / (nMaxAY - nMinY);
					if (gl.curChromB.CA[i] == -999) break;
				}
				else if (nCh == 2)
				{
					pt.y = rt.bottom - (gl.curChromB.CB[i] - nMinY)*nHeight*1.0 / (nMaxBY - nMinY);
					if (gl.curChromB.CB[i] == -999) break;
				}
			}
			else
			{
				if (nCh == 0)
				{
					pt.y = rt.bottom - (gl.curChromA.CA[i] - nMinY)*nHeight*1.0 / (nMaxAY - nMinY);
					if (gl.curChromA.CA[i] == -999) break;
				}
				else if (nCh == 1)
				{
					pt.y = rt.bottom - (gl.curChromB.CA[i] - nMinY)*nHeight*1.0 / (nMaxAY - nMinY);
					if (gl.curChromB.CA[i] == -999) break;
				}
				else
				{
					pt.y = rt.bottom - (gl.curChromA.CB[i] - nMinY)*nHeight*1.0 / (nMaxBY - nMinY);
					if (gl.curChromA.CB[i] == -999) break;
				}
			}

			if (pt.y < rt.top) pt.y = rt.top;
			if (pt.y > rt.bottom) pt.y = rt.bottom;
			if (i == 0)
				pDC->MoveTo(pt);
			else
				pDC->LineTo(pt);
		}
	}
	//绘制时间标
	CPen pPenD(PS_SOLID, 3, RGB(64,0,128));
	pDC->SelectObject(&pPenD);
	if (gl.m_nWorkMode != 2)
		pt.x = rt.left + gl.curChromA.CAIndex*nWidth / gl.MAXPOINTS;
	else
		pt.x = rt.left + gl.curChromB.CAIndex*nWidth / gl.MAXPOINTS;

	pt.y = rt.bottom - 5;
	pDC->MoveTo(pt);
	pt.y = rt.bottom + 5;
	pDC->LineTo(pt);

	pDC->SelectObject(pOldPen);
	pDC->SelectObject(pOldFont);	
}


void CRealTimeChromG::FindMinMax()
{
	double maxA = -999;
	double maxB = -999;
	double min = 999999;
	for (int i = 0; i < gl.curChromA.CAIndex; i++)
	{
		if (maxA < gl.curChromA.CA[i]) maxA = gl.curChromA.CA[i];
		if (maxB < gl.curChromA.CB[i]) maxB = gl.curChromA.CB[i];
		if (min > gl.curChromA.CA[i]) min = gl.curChromA.CA[i];
		if (min > gl.curChromA.CB[i]) min = gl.curChromA.CB[i];
	}
	for (int i = 0; i < gl.curChromB.CAIndex; i++)
	{
		if (maxA < gl.curChromB.CA[i]) maxA = gl.curChromB.CA[i];
		if (maxB < gl.curChromB.CB[i]) maxB = gl.curChromB.CB[i];
		if (min > gl.curChromB.CA[i]) min = gl.curChromB.CA[i];
		if (min > gl.curChromB.CB[i]) min = gl.curChromB.CB[i];
	}
	int nStep = 2;
	if (maxA > 20000) nStep = 10000;
	else if (maxA > 2000) nStep = 1000;
	else if (maxA > 100) nStep = 100;
	else if (maxA > 20) nStep = 10;
	else if (maxA > 10) nStep = 5;

	m_nMaxYA = int(maxA / nStep + 1)*nStep;
	if (m_nMaxYA < 10) m_nMaxYA = 10;
	m_nMinYA = 0;

	nStep = 2;
	if (maxB > 20000) nStep = 10000;
	else if (maxB > 2000) nStep = 1000;
	else if (maxB > 100) nStep = 100;
	else if (maxB > 20) nStep = 10;
	else if (maxB > 10) nStep = 5;
	m_nMaxYB = int(maxB / nStep + 1)*nStep;
	if (m_nMaxYB < 10) m_nMaxYB = 10;
	m_nMinYB = 0;
}


void CRealTimeChromG::OnLButtonDown(UINT nFlags, CPoint point)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值
	m_bMouseDown = TRUE;
	SetCapture();			// 启动鼠标捕捉
	c_ptDown = point;

	CWnd::OnLButtonDown(nFlags, point);
}


void CRealTimeChromG::OnLButtonUp(UINT nFlags, CPoint point)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值
	m_bMouseDown = FALSE;
	ReleaseCapture();				// 释放鼠标捕捉
	c_ptLast = point;

	if (m_nZoomMode == 1)			//缩放的处理
	{
		CaculateZoom(c_ptDown, c_ptLast);
	}

	Invalidate();
	CWnd::OnLButtonUp(nFlags, point);
}

void CRealTimeChromG::OnMouseMove(UINT nFlags, CPoint point)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值
	if (m_nZoomMode == 1 && m_bMouseDown)
	{
		CClientDC dc(this);
		DrawDynamicRect(&dc, c_ptDown, c_ptLast, point);
	}
	c_ptLast = point;
	CWnd::OnMouseMove(nFlags, point);
}

////////////////////////////////////////////////////////////////////////////////////////////////
//缩放操作
void CRealTimeChromG::DrawDynamicRect(CDC *pDC, CPoint ptBegin, CPoint ptOld, CPoint ptNew)
{
	int nOldDrawMode;
	//设置光栅操作模式:
	nOldDrawMode = pDC->SetROP2(R2_XORPEN);

	CPen	curPen, *pOldPen;
	if (!curPen.CreatePen(PS_SOLID, 1, RGB(0, 255, 0)))
		return;
	pOldPen = (CPen*)pDC->SelectObject(&curPen);
	//坐标转换
	pDC->DPtoLP(&ptBegin);
	pDC->DPtoLP(&ptOld);
	pDC->DPtoLP(&ptNew);

	//消隐旧虚线
	if (ptBegin.x < ptOld.x)
	{
		//绘制这样的"]"矩形			*这一段代码可以不使用分枝.
		pDC->MoveTo(ptBegin);
		pDC->LineTo(ptOld.x, ptBegin.y);
		pDC->LineTo(ptOld);
		pDC->LineTo(ptBegin.x, ptOld.y);
		pDC->LineTo(ptBegin);
	}
	else
	{
		//绘制这样的"["矩形
		pDC->MoveTo(ptBegin);
		pDC->LineTo(ptOld.x, ptBegin.y);
		pDC->LineTo(ptOld);
		pDC->LineTo(ptBegin.x, ptOld.y);
		pDC->LineTo(ptBegin);
	}

	//画新虚线
	if (ptBegin.x < ptNew.x)
	{
		//绘制这样的"]"矩形			*这一段代码可以不使用分枝.
		pDC->MoveTo(ptBegin);
		pDC->LineTo(ptNew.x, ptBegin.y);
		pDC->LineTo(ptNew);
		pDC->LineTo(ptBegin.x, ptNew.y);
		pDC->LineTo(ptBegin);
	}
	else
	{
		//绘制这样的"["矩形
		pDC->MoveTo(ptBegin);
		pDC->LineTo(ptNew.x, ptBegin.y);
		pDC->LineTo(ptNew);
		pDC->LineTo(ptBegin.x, ptNew.y);
		pDC->LineTo(ptBegin);
	}

	pDC->SelectObject(pOldPen);
	pDC->SetROP2(nOldDrawMode);
}


void CRealTimeChromG::CaculateZoom(CPoint ptBegin, CPoint ptEnd)
{
	if (ptBegin.y > ptEnd.y)	//还原
	{
		m_nMaxYA = gl.gcMachine.nYMax[0];
		m_nMaxYB = gl.gcMachine.nYMax[1];
		m_nMinYA = gl.gcMachine.nYMin[0];
		m_nMinYB = gl.gcMachine.nYMin[1];
		m_nZoomMode = 1;
		Invalidate();
	}
	else
	{
		long nMaxYA = GetPointY(ptBegin, 0);		//坐标轴的刻度设置
		long nMaxYB = GetPointY(ptBegin, 1);
		long nMinYA = GetPointY(ptEnd, 0);
		long nMinYB = GetPointY(ptEnd, 1);
		if (nMaxYA > nMinYA)		//缩放
		{
			m_nMaxYA = nMaxYA;
			m_nMinYA = nMinYA;
			m_nMaxYB = nMaxYB;
			m_nMinYB = nMinYB;
			Invalidate();
		}
	}
}

float	CRealTimeChromG::GetPointY(CPoint pt, int nYIndex)
{
	if (m_rcArea.Height() == 0) return 0;
	float dY = 0;
	if (nYIndex == 0)
		dY = m_nMinYA + (m_nMaxYA - m_nMinYA)*(-pt.y + m_rcArea.bottom) / m_rcArea.Height();
	else
		dY = m_nMinYB + (m_nMaxYB - m_nMinYB)*(-pt.y + m_rcArea.bottom) / m_rcArea.Height();
	return dY;
}

void	CRealTimeChromG::RefreshGraph()
{
	CRect rt;
	GetClientRect(rt);
	InvalidateRect(rt);
	UpdateWindow();
}

int CRealTimeChromG::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CWnd::OnCreate(lpCreateStruct) == -1)
		return -1;

	// TODO:  在此添加您专用的创建代码
	m_tOld= CTime::GetCurrentTime();
	m_nTimer = SetTimer(9001, 250, NULL);
	return 0;
}

void CRealTimeChromG::OnTimer(UINT_PTR nIDEvent)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值
	CTime tC = CTime::GetCurrentTime();
	CTimeSpan tTSpan = tC - m_tOld;
	if (m_nTimer == nIDEvent && gl.m_bConnChrom)
	{
		if (tTSpan.GetTotalSeconds() > 8 && m_nTicks > 20 && ((gl.curChromA.CAIndex > 40 && gl.m_nWorkMode != 2) || (gl.curChromB.CAIndex > 40 && gl.m_nWorkMode == 2)))
		{
			if (gl.gcMachine.nYAuto == 1)
				FindMinMax();
			else
			{
				m_nMaxYA = gl.gcMachine.nYMax[0];
				m_nMaxYB = gl.gcMachine.nYMax[1];
				m_nMinYA = 0;
				m_nMinYB = 0;
			}
			m_nTicks = 0;
		}
		else
			m_nTicks++;

		Invalidate();
	}
	CWnd::OnTimer(nIDEvent);
}
